﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BookClubIt.DTO;
namespace BookClubIt
{
    public partial class startbookclub : BasePage
    {
    
        protected void Page_Load(object sender, EventArgs e)
        {

            DTO.BookClub club = BookClubProcessorInstance.GetClubTypes();

            imgClubAvatar.ImageUrl = "avatars/club/default_avatar.png";

            DTO.GenreTypes get_genres = BookClubProcessorInstance.GetGenreTypes();

            ddlGenres.DataSource = get_genres;
            ddlGenres.DataTextField = "GenreName";
            ddlGenres.DataValueField = "Id";
            ddlGenres.DataBind();
        }



        protected void btnCreateClub_Click(Object sender, EventArgs e)
        {
        
            int NonPolls = 0;
            int NonEvents = 0;
            int AdultGroup = 0;
            int NonTopics = 0;
            int RealMeet = 0;

            if (chkNonModAddEvents.Checked)
                NonEvents = 1;

            if (chk18andOver.Checked)
                AdultGroup = 1;

            if (chkNonModAddPolls.Checked)
                NonPolls = 1;


            if (chkNonModAddTopics.Checked)
                NonTopics = 1;

            if (chkRealMeet.Checked)
                RealMeet = 1;

            int selectedClubType = Convert.ToInt32(rblClubType.SelectedValue);
            int selectedGenre = Convert.ToInt32(ddlGenres.SelectedItem.Value);
       //     int selectedGenreType = Convert.ToInt32(ddlGenres.SelectedValue);
  
            BookClub bookclub = new BookClub
            {
                Name = txtClubName.Text.ToString(),
                ClubType = selectedClubType,
                GenreType = selectedGenre,
                CreatedDate = DateTime.Now,
                Description = txtDescription.InnerText.ToString(),
                Rules = txtRules.InnerText.ToString(),
                Website = txtWebsite.Text.ToString(),
                AdultGroup = AdultGroup,
                NonLeaderAddEvents = NonEvents,
                NonLeaderAddPolls = NonPolls,
                NonLeaderAddTopics = NonTopics,
                GroupMeetsRealLife = RealMeet,
                AvatarURL = imgClubAvatar.ImageUrl.ToString().Split('/').Last()

            };
            
            BookClubProcessorInstance.SaveBookClub(bookclub);


            int new_bid = bookclub.Id;

          //  BookProcessorInstance.UpdateBookClubAvatar(new_bid, filename);

            string continueUrl = "bookclub/default.aspx?id=" + new_bid + "&start=1";
            BookClubProcessorInstance.AddClubActivity(bookclub.Id, CurrentUserId, ActivityTypeList.CREATED.ToString(), CID);
            BookClubProcessorInstance.JoinBookClub(bookclub.Id, CurrentUserId, true);
            BookClubProcessorInstance.AddLeaderToClub(bookclub.Id, CurrentUserId);
            BookClubProcessorInstance.AddAdminToClub(bookclub.Id, CurrentUserId);
            Response.Redirect(continueUrl);


            //   DTO.BookClub bookclub = BookProcessorInstance.SaveBookClub(Name, ClubType, GenreType, CreatedDate.InnerText.ToString(), Description, Rules, ReferenceId, Website, AdultGroup, NonLeaderAddEvents, NonLeaderAddPolls, NonLeaderAddTopics, GroupMeetsRealLife);
            //     Page.Response.Redirect(Page.Request.Url.ToString(), true);
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {

            string filename = UploadImage.FileName;
            if (UploadImage.PostedFile.ContentLength != 0)
            {
                UploadImage.SaveAs(Server.MapPath("~/avatars/club/" + filename));
                imgClubAvatar.Visible = true;
                imgClubAvatar.ImageUrl = "avatars/club/"+filename;
              
            }
        }
    }
}